package com.meilai.project.util;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author 张驰
 * @date 2021/3/11 17:00
 */
public class SortMapUtil {
    /**
     * 按key升序排列
     * @param map 原始map
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, String> sortMap = new TreeMap<String, String>(new Comparator<String>() {
            public int compare(String obj1, String obj2) {
                return obj1.compareTo(obj2);//升序排序
            }
        });
        sortMap.putAll(map);
        return sortMap;
    }
}
